<template>
<el-dialog
  title="提示"
  :visible.sync="visible"
  @close="close"
  :show="show"
  append-to-body
  width="500px">
  <slot></slot>
  <input
    type="file"
    @change="importf()"
    ref="pathClear"
    accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
  />
  <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="uploadExcel">导 入</el-button>
  </span>
</el-dialog>
</template>
<script>
import XLSX from "xlsx";
export default {
  props:{
    show: {
        type: Boolean,
        default: false
    }

  },
  data() {
    return {
      excelData:[],
      visible: this.show,
    };
  },
  mounted() {
    // window.console.log(this.dialogUploadExcel);
  },
  methods: {
    // 导入
    importf() {
      
      let _this = this;
      // _this.$refs. pathClear.value = "";
      _this.excelData = [];
      this.file = event.currentTarget.files[0];
      var rABS = false; //是否将文件读取为二进制字符串
      var file = this.file;

      FileReader.prototype.readAsBinaryString = function(f) {
        var binary = "";
        var rABS = false; //是否将文件读取为二进制字符串
        // var pt = this;
        var workbook; //读取完成的数据
        // var excelData;
        var reader = new FileReader();
        reader.onprogress = function(e) {
          let total = file.size;
          _this.progress = (e.loaded / total) * 100;
          // window.console.log(_this.progress);
        };
        reader.onload = function() {
          try {
            var bytes = new Uint8Array(reader.result);
            var length = bytes.byteLength;
            for (var i = 0; i < length; i++) {
              binary += String.fromCharCode(bytes[i]);
            }
            // window.console.log(fixdata(binary))
            if (rABS) {
              workbook = XLSX.read(btoa(), {
                //手动转化
                type: "base64"
              });
            } else {
              workbook = XLSX.read(binary, {
                type: "binary"
              });
            }
            // excelData = [];
          } catch (e) {
            window.console.log("文件类型不正确");
            return;
          }
          let fromTo = "";
          for (var sheet in workbook.Sheets) {
            if (workbook.Sheets.hasOwnProperty(sheet)) {
              fromTo = workbook.Sheets[sheet]["!ref"];
              if(fromTo){
              _this.excelData.push(
                XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
              );
              }
            }
          }
          // window.console.log(_this.excelData);
        };
        reader.readAsArrayBuffer(f);
      };

      var reader = new FileReader();
      if (rABS) {
        reader.readAsArrayBuffer(file);
      } else {
        reader.readAsBinaryString(file);
      }
    },
    // 传父组件
    uploadExcel(){
        
        if(this.excelData.length>0){
          this.$emit('getUpload', this.excelData);
          this.$emit('update:show', false);
          this.$refs. pathClear.value = "";
        }else{
          this.$message.error('请选择文件');
        }
    },
    close(){
      this.$emit('update:show', false);
      this.$refs.pathClear.value = '';
    },
  },
  components: {},
  watch: {
      show () {
          this.visible = this.show;
      }
  }

};
</script>